﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Labb2
{
    class Explosion
    {
        Texture2D m_explosionSpriteSheet;
        Vector2 m_startPosition;
        float m_maxTime;
        int m_numberOfFrames;
        int m_numberOfFramesX;
        int fSize;
        float m_timeElapsed;

        public Explosion(Vector2 startPosition) 
        {
            m_startPosition = startPosition;
            m_maxTime = 0.5f;
            m_numberOfFrames = 24;
            m_numberOfFramesX = 4;
            fSize = 128;
        }

        public void LoadContent(ContentManager contManager) 
        {
            m_explosionSpriteSheet = contManager.Load<Texture2D>("explosion");
        }

        public void Draw(SpriteBatch spritebatch, GameTime gametime)
        {
            //float maxTime = 0.5f;
            m_timeElapsed += (float)gametime.ElapsedGameTime.TotalSeconds;
            float percentAnimated = m_timeElapsed / m_maxTime;
            
            int frame = (int)(percentAnimated * m_numberOfFrames);

            float frameX = frame % m_numberOfFramesX;
            float frameY = (float)frame / m_numberOfFramesX;

            //spritebatch.Draw(m_explosionSpriteSheet, new Rectangle((int)m_startPosition.X, (int)m_startPosition.Y, 256, 256), null, Color.White);
            Rectangle destinationRect = new Rectangle((int)m_startPosition.X - fSize / 2, (int)m_startPosition.Y - fSize / 2, fSize, fSize);
            Rectangle sourceRect = new Rectangle((int)frameX * fSize, (int)frameY * fSize, fSize, fSize);
            spritebatch.Draw(m_explosionSpriteSheet, destinationRect, sourceRect, Color.White);
            System.Diagnostics.Debug.WriteLine(m_timeElapsed);
            
            if(m_timeElapsed > 0.5f)
            {
                m_timeElapsed = 0;
            }
        }
    }
}
